<?php

namespace App\backend\controllers\commission;


use App\backend\controllers\AdminBaseController;
use App\backend\models\AgentLevel;
use App\backend\services\AgentLevelServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Utils\JsonResp;

class AgentLevelController extends AdminBaseController
{
    protected $agentLevelServices;

    public function __construct(AgentLevelServices $agentLevelServices)
    {
        $this->agentLevelServices = $agentLevelServices;
    }

    /**
     * 分销商等级列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function agentLevelLists(Request $request)
    {
        $request = $request->all();
        $result = $this->agentLevelServices->agentLevelLists($request);
        foreach ($result['data'] as $item){
            $item->upgrade_strategy = json_decode($item->upgrade_strategy,true);
            $item->status_text = AgentLevel::$statusMap[$item->status];
            $item->is_reduce_text = AgentLevel::$isReduceMap[$item->is_reduce];
            $item->level_text = AgentLevel::LEVEL_MAP[$item->level];
        }
        return successJson('成功！', $result);
    }

    /**
     * 分销商等级详情
     *
     * @param  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function agentLevelDetail($id)
    {
        $result = $this->agentLevelServices->agentLevelDetail($id);
        $result->upgrade_strategy = json_decode($result->upgrade_strategy,true);
        $result->is_reduce_text = AgentLevel::$isReduceMap[$result->is_reduce];
        $result->is_reduce_text = AgentLevel::$isReduceMap[$result->is_reduce];
        $result->level_text = AgentLevel::LEVEL_MAP[$result->level];
        return successJson('成功！', $result);
    }

    /**
     * 分销商等级下拉
     */
    public function getAgentLevel()
    {
        return JsonResp::success('成功', AgentLevel::$levelArr);
    }

    /**
     * 升级策略下拉
     */
    public function getUpgradeStrategy()
    {
        return JsonResp::success('成功', AgentLevel::$upgradeStrategyArr);
    }

    /**
     * 降级策略下拉
     */
    public function getFallStrategy()
    {
        return JsonResp::success('成功', AgentLevel::$fallStrategyArr);
    }

    /**
     * 分销商等级添加
     */
    public function addAgentLevel(Request $request)
    {
        $request = $request->all();
        $validator = Validator::make(
            $request,
            AgentLevel::rules(),
            AgentLevel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $result = $this->agentLevelServices->addAgentLevel($request);
        return successJson('成功！', $result);
    }

    /**
     * 分销商等级删除
     */
    public function deleteAgentLevel(Request $request)
    {
        $result = $this->agentLevelServices->deleteAgentLevel($request);
        return successJson('成功！', $result);
    }


    /**
     * 分销商等级修改
     */
    public function editAgentLevel(Request $request)
    {
        $request = $request->all();
        $validator = Validator::make(
            $request,
            AgentLevel::rules(),
            AgentLevel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $result = $this->agentLevelServices->editAgentLevel($request);
        return successJson('成功！', $result);
    }

    /**
     * 分销商等级启用
     */
    public function setLevelStatus(Request $request)
    {
        $request = $request->all();
        $result = $this->agentLevelServices->setLevelStatus($request);
        return successJson('成功！', $result);
    }

}
